package com.my_musc_service;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import com.baomidou.mybatisplus.core.toolkit.AES;
import com.my_musc_service.config.LogBackConfigLoader;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Objects;

/**
 * @author Zhu Dunfeng
 * @date 2021/8/19 9:56
 */
@Slf4j
@SpringBootTest
public class EncryptorTeset {
//    static{
//        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
//        JoranConfigurator configurator = new JoranConfigurator();
//        configurator.setContext(lc);
//        lc.reset();
//        try {
//            configurator.doConfigure("D:/IdeaProjects/my_musc_service/my_musc_service/src/main/resources/logback-spring.xml");//加载logback配置文件
//        } catch (JoranException e) {
//            e.printStackTrace();
//        }
//        //PropertyConfigurator.configure("/home/lhy/Workspaces/MyEclipse_data/gooddeep/src/main/java/config/log4j.properties");//加载logj配置文件
//    }

//    private static final Logger logger = LoggerFactory.getLogger(EncryptorTeset.class);
    @Autowired
    private StringEncryptor stringEncryptor;

    public void encrypt(String content) {
//        try {
//            LogBackConfigLoader.load(Objects.requireNonNull(EncryptorTeset.class.getClassLoader().getResource("logback-spring.xml")).getPath());
//        } catch (IOException | JoranException e) {
//            e.printStackTrace();
//        }
        String encryptStr = stringEncryptor.encrypt(content);
        System.out.println("加密后的内容：" + encryptStr);
        log.info("加密后的内容：" + encryptStr);
    }

    @Test
    public void encryptString(){
//        String input="root";
        String input="10.0.24.11";
        encrypt(input);
    }

    @Test
    public void haha(){
//        try {
//            LogBackConfigLoader.load(Objects.requireNonNull(EncryptorTeset.class.getClassLoader().getResource("logback-spring.xml")).getPath());
//        } catch (IOException | JoranException e) {
//            e.printStackTrace();
//        }
        System.out.println("哈哈");
        log.info("哈哈");
    }

    @Test
    public void mybatisPlusAES(){
        // 生成 16 位随机 AES 密钥
        String randomKey = AES.generateRandomKey();
        System.out.println(randomKey);

        // 随机密钥加密
        String result = AES.encrypt("哈哈哈", randomKey);
        System.out.println(result);

        //密钥解密
        String decrypt = AES.decrypt(result, randomKey);
        System.out.println(decrypt);
    }

}
